package com.zyy.game.tianlong.constant;

import com.zyy.game.tianlong.network.exception.LogicException;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 每局的作弊牌堆(设置必胜牌堆)
 *
 * @author relufi
 * @date 2019/8/5 0005
 */
public class CardCheatGroup {
    private final Card[][] cards;
    private final int maxIndex;
    private AtomicInteger index = new AtomicInteger();
    /**
     * 预制普通固定牌，在设置一位玩家必胜时，其他玩家使用固定牌 （包括 高牌 对子 顺子 金花 小同花顺）
     */
    private static final Card[][] UN_LUCKY = new Card[10][];

    static {
        UN_LUCKY[0] = new Card[]{Card.index(5, 2), Card.index(5, 1), Card.index(6, 0)};
        UN_LUCKY[1] = new Card[]{Card.index(4, 0), Card.index(5, 0), Card.index(9, 0)};
        UN_LUCKY[2] = new Card[]{Card.index(3, 3), Card.index(5, 3), Card.index(10, 3)};
        UN_LUCKY[3] = new Card[]{Card.index(12, 2), Card.index(12, 1), Card.index(6, 2)};
        UN_LUCKY[4] = new Card[]{Card.index(12, 3), Card.index(11, 1), Card.index(10, 2)};
        UN_LUCKY[5] = new Card[]{Card.index(1, 3), Card.index(2, 3), Card.index(7, 3)};
        UN_LUCKY[6] = new Card[]{Card.index(6, 1), Card.index(7, 1), Card.index(8, 1)};
        UN_LUCKY[7] = new Card[]{Card.index(13, 1), Card.index(7, 2), Card.index(2, 2)};
        UN_LUCKY[8] = new Card[]{Card.index(1, 1), Card.index(11, 3), Card.index(9, 1)};
        UN_LUCKY[9] = new Card[]{Card.index(9, 3), Card.index(3, 1), Card.index(2, 0)};
        for (Card[] cards : UN_LUCKY) {
            CardUtils.sort(cards);
        }
    }

    /**
     * 预制幸运牌，设置玩家必赢时从此牌堆中取牌
     */
    private static final Card[][] LUCKY = new Card[4][];

    static {
        LUCKY[0] = new Card[]{Card.index(8, 3), Card.index(8, 2), Card.index(8, 0)};
        LUCKY[1] = new Card[]{Card.index(13, 0), Card.index(13, 2), Card.index(13, 3)};
        LUCKY[2] = new Card[]{Card.index(4, 3), Card.index(4, 2), Card.index(4, 1)};
        LUCKY[3] = new Card[]{Card.index(10, 0), Card.index(11, 0), Card.index(12, 0)};
        for (Card[] cards : LUCKY) {
            CardUtils.sort(cards);
        }
    }

    public CardCheatGroup(int user) {
        if (user <= 1 || user > 6) {
            throw new LogicException("牌组人数不正确");
        }
        this.maxIndex = user - 1;
        cards = new Card[maxIndex][];
        int count = 0;
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        while (count < maxIndex) {
            Card[] card = UN_LUCKY[rnd.nextInt(10)];
            boolean isExits = false;
            for (int i = 0; i < count; i++) {
                if (cards[i] == card) {
                    isExits = true;
                    break;
                }
            }
            if (!isExits) {
                cards[count] = card;
                count++;
            }
        }
    }

    public CardCheatGroup() {
        this(6);
    }

    /**
     * 发牌  普通牌
     *
     * @return 固定的3张牌，从大到小排序 null:在牌堆空时返回null
     */
    public Card[] deal() {
        int index = this.index.getAndIncrement();
        if (index >= maxIndex) {
            return null;
        }
        return cards[index];
    }

    /**
     * 发牌 幸运牌
     *
     * @return 固定的幸运牌组
     */
    public Card[] dealLucky() {
        return LUCKY[ThreadLocalRandom.current().nextInt(4)];
    }


    public boolean isEnd() {
        return index.get() >= maxIndex;
    }

    @Override
    public String toString() {
        return "index =" + index.get() + " cards=" + Arrays.deepToString(cards);
    }

}
